﻿//AcademicPlanner - course registration planning web app.
//Copyright (C) 2009  Boubacar Diallo

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AcademicPlanner.Data;
using AcademicPlanner.Data.Entities;
using AcademicPlanner.Data.Repositories;

namespace AcademicPlanner.Domain.Models.Collections
{
	/// <summary>
	/// RegistrationCollection represents a collection of <c>Registration</c>s.
	/// </summary>
	/// <since>1.0</since>
	public class RegistrationCollection : ModelCollection<Registration, RegistrationEntityRepository, RegistrationEntity>
	{
		///// <summary>
		///// The format of keys to the <c>IDictionary</c> returned by <see cref="M:ToDictionary"/>.
		///// </summary>
		///// <remarks>
		///// There are three placeholders for respectively the year of the <c>Registration</c>,
		///// the <c>OrderNum</c> of its <c>Term</c> and the identifier of its <c>Course</c>.
		///// </remarks>
		///// <since>1.0</since>
		internal static readonly string toDictionaryKeyFormat = "{0}-{1}-{2}";

		/// <summary>
		/// Gets the data layer repository of the underlying <c>RegistrationEntity</c>s of this collection's <c>Registration</c>s.
		/// </summary>
		/// <since>1.0</since>
		protected override RegistrationEntityRepository EntityRepo { get { return DomainMaster.DSAS.RegistrationEntityRepo; } }


		internal RegistrationCollection()
			: base()
		{

		}

		internal RegistrationCollection(IList<RegistrationEntity> pEntities)
			: base(pEntities)
		{

		}

		internal IDictionary<string, Registration> ToDictionary()
		{
			IDictionary<string, Registration> rDic = new Dictionary<string, Registration>();
			var vEntitiesList = entities as IList<RegistrationEntity>;

			if (entities != null && ((ICollection)vEntitiesList) != null)
			{
				foreach (var vEnt in vEntitiesList)
				{
					if (!vEnt.Trashed)
					{
						var vMod = DomainMaster.ModelsFountain.GetModel<Registration>(vEnt);
						var vKey = string.Format(toDictionaryKeyFormat, vMod.Year, vMod.Term.OrderNum, 
													vMod.Course.EntityId);

						rDic.Add(vKey, vMod);
					}
				}
			}

			return rDic;
		}

		internal RegistrationCollection AnteriorTo(int pYear, int pTermOrderNum)
		{
			RegistrationCollection rModCollection = null;
			IList<RegistrationEntity> vEnts = new List<RegistrationEntity>();

			try
			{
				if (entities != null)
					vEnts = EntityRepo.FilterByAnteriorPeriodEntity(entities, pYear, pTermOrderNum);
			}
			catch (DataException vDx)
			{
				var vCx = new CriticalDomainException(DomainErrorCode.M_REGISTRATIONENTITY_RETRIEVAL_BY_ANTERIOR_PERIODENTITY_FAILURE, vDx);
				throw vCx;
			}

			rModCollection = new RegistrationCollection(vEnts);

			return rModCollection;
		}
	}
}
